<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
        >
            <el-form-item label="姓名" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入姓名"
                    clearable
                    style="width: 200px"
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="月份" prop="month">
                <el-select
                    v-model="queryParams.month"
                    placeholder="请选择月份"
                    clearable
                    style="width: 200px"
                >
                    <el-option
                        v-for="month in monthOptions"
                        :key="month.value"
                        :label="month.label"
                        :value="month.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['system:salary:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['system:salary:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:salary:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['system:salary:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="salaryList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="姓名" align="center" prop="employeeName" />
            <el-table-column label="月份" align="center" prop="month" />
            <el-table-column label="日薪" align="center" prop="dailySalary" />
            <el-table-column label="出勤天数" align="center" prop="attendanceDays" />
            <el-table-column label="扣前应发工资" align="center" prop="grossSalary" />
            <el-table-column label="总支款" align="center" prop="totalPayment" />
            <el-table-column label="社保" align="center" prop="socialSecurity" />
            <el-table-column label="实发工资" align="center" prop="netSalary" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button
                        link
                        type="primary"
                        icon="Edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:salary:edit']"
                    >修改</el-button>
                    <el-button
                        link
                        type="primary"
                        icon="Delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['system:salary:remove']"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改薪资对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="salaryRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="月份" prop="month">
                    <el-select v-model="form.month" placeholder="请选择月份" style="width: 100%">
                        <el-option
                            v-for="month in monthOptions"
                            :key="month.value"
                            :label="month.label"
                            :value="month.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="日薪" prop="dailySalary">
                    <el-input-number
                        v-model="form.dailySalary"
                        :precision="2"
                        :step="0.1"
                        :min="0"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="出勤天数" prop="attendanceDays">
                    <el-input-number
                        v-model="form.attendanceDays"
                        :min="0"
                        :max="31"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="扣前应发工资" prop="grossSalary">
                    <el-input-number
                        v-model="form.grossSalary"
                        :precision="2"
                        :step="0.1"
                        :min="0"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="总支款" prop="totalPayment">
                    <el-input-number
                        v-model="form.totalPayment"
                        :precision="2"
                        :step="0.1"
                        :min="0"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="社保" prop="socialSecurity">
                    <el-input-number
                        v-model="form.socialSecurity"
                        :precision="2"
                        :step="0.1"
                        :min="0"
                        style="width: 100%"
                    />
                </el-form-item>
                <el-form-item label="实发工资" prop="netSalary">
                    <el-input-number
                        v-model="form.netSalary"
                        :precision="2"
                        :step="0.1"
                        :min="0"
                        style="width: 100%"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Salary">
import { listSalary, getSalary, delSalary, addSalary, updateSalary, exportSalary } from '@/api/system/salary'

const { proxy } = getCurrentInstance()

const salaryList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

// 月份选项
const monthOptions = ref([
    { value: '1', label: '1月' },
    { value: '2', label: '2月' },
    { value: '3', label: '3月' },
    { value: '4', label: '4月' },
    { value: '5', label: '5月' },
    { value: '6', label: '6月' },
    { value: '7', label: '7月' },
    { value: '8', label: '8月' },
    { value: '9', label: '9月' },
    { value: '10', label: '10月' },
    { value: '11', label: '11月' },
    { value: '12', label: '12月' }
])

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        month: undefined
    },
    rules: {
        name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        month: [
            { required: true, message: '月份不能为空', trigger: 'change' }
        ],
        dailySalary: [
            { required: true, message: '日薪不能为空', trigger: 'blur' }
        ],
        attendanceDays: [
            { required: true, message: '出勤天数不能为空', trigger: 'blur' }
        ],
        grossSalary: [
            { required: true, message: '扣前应发工资不能为空', trigger: 'blur' }
        ],
        totalPayment: [
            { required: true, message: '总支款不能为空', trigger: 'blur' }
        ],
        socialSecurity: [
            { required: true, message: '社保不能为空', trigger: 'blur' }
        ],
        netSalary: [
            { required: true, message: '实发工资不能为空', trigger: 'blur' }
        ]
    }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询薪资列表 */
function getList() {
    loading.value = true
    listSalary(queryParams.value).then(response => {
        salaryList.value = response.rows
        total.value = response.total
        loading.value = false
    })
}

/** 取消按钮 */
function cancel() {
    open.value = false
    reset()
}

/** 表单重置 */
function reset() {
    form.value = {
        salaryId: undefined,
        name: undefined,
        month: undefined,
        dailySalary: undefined,
        attendanceDays: undefined,
        grossSalary: undefined,
        totalPayment: undefined,
        socialSecurity: undefined,
        netSalary: undefined
    }
    proxy.resetForm('salaryRef')
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef')
    handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.salaryId)
    single.value = selection.length != 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = '添加薪资'
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset()
    const salaryId = row.salaryId || ids.value
    getSalary(salaryId).then(response => {
        form.value = response.data
        open.value = true
        title.value = '修改薪资'
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['salaryRef'].validate(valid => {
        if (valid) {
            if (form.value.salaryId != undefined) {
                updateSalary(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功')
                    open.value = false
                    getList()
                })
            } else {
                addSalary(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功')
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const salaryIds = row.salaryId || ids.value
    proxy.$modal.confirm('是否确认删除薪资编号为"' + salaryIds + '"的数据项？').then(function() {
        return delSalary(salaryIds)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
    }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('system/salary/export', {
        ...queryParams.value
    }, `salary_${new Date().getTime()}.xlsx`)
}

getList()
</script> 